﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Common.Constants;
using Entities;
using Common.Helper;
namespace DataAccess
{
    public class BusinessDataAccess
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static BusinessDataAccess Instance { get; private set; }
        /// <summary>
        /// Initializes the <see cref="ContactDataAccess" /> class.
        /// </summary>
        static BusinessDataAccess() { Instance = new BusinessDataAccess(); }



        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static Business ExtractRawData(IDataRecord dataReader)
        {
            var business = new Business
                               {
                                   BusinessId = (Guid) dataReader["BusinessID"],
                                   BusinessName = dataReader["BusinessName"].ToString(),
                                   SicCode = dataReader["SICCode"].ToString()
                               };

            return business;


        }

        /// <summary>
        /// Lists the contact.
        /// </summary>
        /// <param name="bussinessName">Name of the bussiness.</param>
        /// <param name="sicCode">The sic code.</param>
        /// <returns></returns>
        public Collection<Business> ListBusiness(string bussinessName, string sicCode)
        {
            var lstBusiness = new Collection<Business>();

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("businessName", bussinessName),
                                       new SqlParameter("sicCode",sicCode)
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.ListBusiness, colParameter))
            {
                while (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    lstBusiness.Add(result);
                }
                return lstBusiness;
            }
        }
        public Business GetBusiness(string bussinessId)
        {
           

            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("businessId ", bussinessId)
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetBusiness, colParameter))
            {
                if (reader.Read())
                {
                    var result = ExtractRawData(reader);
                    return result;
                }
                return null;
            }
        }
    }
}
